package com.example.plugindemo

import android.content.Context
import android.content.ContextWrapper
import android.content.res.AssetManager
import android.content.res.Resources
import android.content.res.Resources.Theme
import android.view.LayoutInflater
import java.io.File

class PluginContext(
    base: Context?,
    private val remoteContext: Context,
    private val mPluginClassLoader: ClassLoader?
) : ContextWrapper(base) {
    private var mInflater: LayoutInflater? = null

    override fun getClassLoader(): ClassLoader? {
        return mPluginClassLoader
    }

    override fun getResources(): Resources? {
        return remoteContext.resources
    }

    override fun getTheme(): Theme? {
        return remoteContext.theme
    }

    override fun getPackageName(): String? {
        return remoteContext.packageName
    }

    override fun isRestricted(): Boolean {
        return remoteContext.isRestricted
    }

    override fun getCacheDir(): File? {
        return remoteContext.cacheDir
    }

    override fun getFilesDir(): File? {
        return remoteContext.filesDir
    }

    override fun getDataDir(): File? {
        return remoteContext.dataDir
    }

    override fun getSystemService(name: String): Any? {
        if (LAYOUT_INFLATER_SERVICE == name) {
            if (mInflater == null) {
                mInflater = LayoutInflater.from(baseContext).cloneInContext(this)
            }
            return mInflater
        }
        return baseContext.getSystemService(name)
    }

    override fun getAssets(): AssetManager? {
        return remoteContext.assets
    }

}